package com.ruoyi.merchant.controller;

import cn.hutool.core.lang.Validator;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.merchant.domain.ImsVehCusUnit;
import com.ruoyi.merchant.service.IImsVehCusUnitService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户单位Controller
 * 
 * @author ruoyi
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/cusunit/vehcusunit")
public class ImsVehCusUnitController extends BaseController
{
    @Autowired
    private IImsVehCusUnitService imsVehCusUnitService;

    /**
     * 查询客户单位列表
     */
    //@PreAuthorize("@ss.hasPermi('cusunit:vehcusunit:list')")
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(ImsVehCusUnit imsVehCusUnit)
    {
        startPage();
        List<ImsVehCusUnit> list = imsVehCusUnitService.selectImsVehCusUnitList(imsVehCusUnit);
        return getDataTable(list);
    }

    /**
     * 导出客户单位列表
     */
//    @PreAuthorize("@ss.hasPermi('cusunit:vehcusunit:export')")
    @Log(title = "客户单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImsVehCusUnit imsVehCusUnit)
    {
        List<ImsVehCusUnit> list = imsVehCusUnitService.selectImsVehCusUnitList(imsVehCusUnit);
        ExcelUtil<ImsVehCusUnit> util = new ExcelUtil<ImsVehCusUnit>(ImsVehCusUnit.class);
        util.exportExcel(response, list, "客户单位数据");
    }

    /**
     * 获取客户单位详细信息
     */
    //@PreAuthorize("@ss.hasPermi('cusunit:vehcusunit:query')")
    @GetMapping(value = "/{id}")
    @Anonymous
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imsVehCusUnitService.selectImsVehCusUnitById(id));
    }

    /**
     * 新增客户单位
     */
    //@PreAuthorize("@ss.hasPermi('cusunit:vehcusunit:add')")
    @Log(title = "客户单位", businessType = BusinessType.INSERT)
    @PostMapping
    @Anonymous
    public AjaxResult add(@RequestBody ImsVehCusUnit imsVehCusUnit)
    {
        AjaxResult ajax=AjaxResult.success();
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getPersonTel())) {
            if (!Validator.isMobile(imsVehCusUnit.getPersonTel())) {
                return error("手机号不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getPersonEmail())) {
            if (!Validator.isEmail(imsVehCusUnit.getPersonEmail())) {
                return error("邮箱不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getPersonIdCard())) {
            if (!Validator.isCitizenId(imsVehCusUnit.getPersonIdCard())) {
                return error("身份证号码不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getComContactIdCard())) {
            if (!Validator.isCitizenId(imsVehCusUnit.getComContactIdCard())) {
                return error("法人身份证号码不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getComEmail())) {
            if (!Validator.isEmail(imsVehCusUnit.getComEmail())) {
                return error("企业邮箱不正确！");
            }
        }
        imsVehCusUnit.setAddTime(DateUtils.getNowDate());
        imsVehCusUnitService.insertImsVehCusUnit(imsVehCusUnit);
        ajax.put("cusUnitId",imsVehCusUnit.getId());
        return ajax;
    }

    /**
     * 修改客户单位
     */
    //@PreAuthorize("@ss.hasPermi('cusunit:vehcusunit:edit')")
    @Log(title = "客户单位", businessType = BusinessType.UPDATE)
    @PutMapping
    @Anonymous
    public AjaxResult edit(@RequestBody ImsVehCusUnit imsVehCusUnit)
    {
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getPersonTel())) {
            if (!Validator.isMobile(imsVehCusUnit.getPersonTel())) {
                return error("手机号不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getPersonEmail())) {
            if (!Validator.isEmail(imsVehCusUnit.getPersonEmail())) {
                return error("邮箱不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getPersonIdCard())) {
            if (!Validator.isCitizenId(imsVehCusUnit.getPersonIdCard())) {
                return error("身份证号码不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getComContactIdCard())) {
            if (!Validator.isCitizenId(imsVehCusUnit.getComContactIdCard())) {
                return error("法人身份证号码不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getComEmail())) {
            if (!Validator.isEmail(imsVehCusUnit.getComEmail())) {
                return error("企业邮箱不正确！");
            }
        }
        return toAjax(imsVehCusUnitService.updateImsVehCusUnit(imsVehCusUnit));
    }

    /**
     * 删除客户单位
     */
    //@PreAuthorize("@ss.hasPermi('cusunit:vehcusunit:remove')")
    @Log(title = "客户单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @Anonymous
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imsVehCusUnitService.deleteImsVehCusUnitByIds(ids));
    }
}
